package com.lianwei.hospital.Service.Impl;

import com.lianwei.hospital.Service.AdmainService;
import com.lianwei.hospital.mapper.AdmainMapper;
import com.lianwei.hospital.module.entity.Admain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service("AdmainService")
public class AdmainImpl implements AdmainService {
    @Autowired
    AdmainMapper admainMapper;

    @Override
    public Admain selectByPrimaryKey(Integer id) throws Exception {
        return admainMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean insert(Admain admain) throws Exception {
        return admainMapper.insert(admain)>0;
    }

    @Override
    public List<Admain> selectAll() throws Exception {
        return admainMapper.selectAll();
    }

    @Override
    public boolean deleteByPrimaryKey(Integer id) throws Exception {
        return admainMapper.deleteByPrimaryKey(id)>0;
    }

    @Override
    public boolean updateByPrimaryKey(Admain admain) throws Exception {
        return admainMapper.updateByPrimaryKey(admain)>0;
    }

    @Override
    public Admain login(String username, String password) throws Exception {
        Admain admain1= admainMapper.findByUsername(username);
        if (admain1 == null){
            throw new Exception("用户未注册");
        }else {
            if (admain1.getAPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes()))){
                return admain1;
            }else {
                throw new Exception("用户名密码错误");
            }
        }
    }
}
